package com.winning.gjhlht.ihe.hl7ClientService.data.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.framework.server.db.bean.PageBean;
import com.framework.server.db.bean.PageQueryBean;
import com.framework.server.manager.SQLConfigLoader;
import com.winning.common.data.service.PlatBasicDataService;
import com.winning.gjhlht.ihe.hl7ClientService.data.dao.inter.IHEDataDaoInter;

/**
 * 类名称：IHEDepartDataService
 * 类描述：   科室上传互联互通服务
 * 创建人：zhaqiang
 * 创建时间：2017-3-7 上午9:28:09
 * 修改人：zhaqiang
 * 修改时间：2017-3-7 上午9:28:09
 * 修改备注：
 */
@Service(value = "com.winning.gjhlht.ihe.hl7ClientService.data.service.IHEDepartDataService")
public class IHEDepartDataService extends IHEBaseDataService {
    private static Logger LOGGER = Logger.getLogger("pbsLoger");

    //张家港科室注册注册服务ID
    private static final String ZJG_DEPART_SERVICE_ID = "DEPART_ZJG_TEMP_002";


    @Autowired
    private PlatBasicDataService platBasicDataService = null;

    @Autowired
    private IHEDataDaoInter iheDataDao = null;


    /**
     * 分页查询医疗科室信息
     * 方法名称: queryKsxxByPage
     *
     * @param
     * @return JSONObject  返回类型说明<br/>
     * @throws <br/>
     * @author zhaqiang@winning.com.cn<br/>
     * 创建时间：2017-3-6/下午2:37:32<br/>
     */
    public JSONObject queryKsxxByPage(Map<String, Object> param, int pageNumber, int pageSize) {
        JSONObject result = new JSONObject();

        try {

            //根据上传状态获取不同查询结果
            if (param.containsKey("sczt")) {
                String sczt = param.get("sczt").toString();

                String sql = "";

                if (sczt != null && !sczt.equals("")) {
                    if (sczt.equals("3")) {
                        sql = SQLConfigLoader.getSQLByID("IHE_Client_QUERY_KSXX_WSC");

                        //添加条件
                        if (param.containsKey("yljgdm")) {
                            sql += " and jgbm = '" + param.get("yljgdm") + "'";
                        }
                        if (param.containsKey("ksdm")) {
                            sql += " and ksdm = '" + param.get("ksdm") + "'";
                        }
                        if (param.containsKey("ksmc")) {
                            sql += " and ksmc like '%" + param.get("ksmc") + "%'";
                        }
                    } else {
                        sql = SQLConfigLoader.getSQLByID("IHE_Client_QUERY_KSXX_YSC");


                        //添加条件
                        if (param.containsKey("yljgdm")) {
                            sql += " and jgbm = '" + param.get("yljgdm") + "'";
                        }
                        if (param.containsKey("ksdm")) {
                            sql += " and ksdm = '" + param.get("ksdm") + "'";
                        }
                        if (param.containsKey("ksmc")) {
                            sql += " and ksmc like '%" + param.get("ksmc") + "%'";
                        }
                        if (param.containsKey("sczt")) {
                            if (param.get("sczt").equals("1")) {
                                sql += " and zcjg = 'AA'";
                            }
                            if (param.get("sczt").equals("2")) {
                                sql += " and zcjg = 'AE'";
                            }
                        }
                    }


                    //数据源
                    String dbDataSourceID = getDBDataSouorceID(ZJG_DEPART_SERVICE_ID);

                    //做分页处理
                    PageBean pageBean = new PageBean();

                    pageBean.setPageNum(pageNumber);

                    //必须要填的
                    pageBean.setPageSize(pageSize);

                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

                    LOGGER.info("--> 开始执行SQL 开始时间：" + formatter.format(new Date().getTime()));

                    PageQueryBean pqb = iheDataDao.executeSQLByPage(pageBean, sql, "", dbDataSourceID);

                    LOGGER.info("--> 结束执行SQL 结束时间：" + formatter.format(new Date().getTime()));


                    if (pqb.getDataSet() != null) {
                        //数据库查询结果集采用XML结构返回
                        String resultXML = pqb.getDataSet().toString();

                        JSONArray array = paraseXMLToJSONArray(resultXML);

                        result.put("rows", array);
                        result.put("total", pqb.getPageBean().getRecordCount());
                    }

                }
            }
        } catch (Exception e) {
            LOGGER.error(e, e.fillInStackTrace());
        }

        return result;
    }


    /**
     * 获取医疗科室上传统计信息
     * 方法名称: queryKsxxSctj
     *
     * @param
     * @return JSONObject  返回类型说明<br/>
     * @throws <br/>
     * @author zhaqiang@winning.com.cn<br/>
     * 创建时间：2017-3-6/下午3:47:49<br/>
     */
    public JSONObject queryKsxxSctj() {
        JSONObject result = new JSONObject();

        try {
            String sql = SQLConfigLoader.getSQLByID("IHE_Client_CountKsxx_SCTJ");

            //数据源
            String dbDataSourceID = getDBDataSouorceID(ZJG_DEPART_SERVICE_ID);

            result = iheDataDao.executSQLForJSONObject(sql, null, dbDataSourceID);

            if (result == null) {
                result = new JSONObject();

                result.put("ZSL", 0);
                result.put("SCCG", 0);
                result.put("SCSB", 0);
                result.put("WSC", 0);
                result.put("YSC", 0);

            }

            result.put("XCZXSJ", getTimeByTrigger(ZJG_DEPART_SERVICE_ID));

        } catch (Exception e) {
            LOGGER.error(e, e.fillInStackTrace());

        }

        return result;
    }


    /**
     * 上传一个科室信息信息
     * 方法名称: uploadKSXXInfo
     *
     * @param
     * @return JSONObject  返回类型说明<br/>
     * @throws <br/>
     * @author zhaqiang@winning.com.cn<br/>
     * 创建时间：2017-3-6/下午4:03:32<br/>
     */
    public JSONObject uploadKSXXInfo(String ksdm) {
        JSONObject json = new JSONObject();

        try {
            String tjSQL = " AND KSDM= '" + ksdm + "'";

            boolean tag = uploadHlhtService(tjSQL, ZJG_DEPART_SERVICE_ID, platBasicDataService, iheDataDao);

            if (tag) {
                String sql = "SELECT KSDM,KSMC,ZCJG,ZCJGSM FROM IHE_SYS_KSXX_LOG WHERE KSDM = '" + ksdm + "'";

                //数据源
                String dbDataSourceID = getDBDataSouorceID(ZJG_DEPART_SERVICE_ID);

                JSONObject result = iheDataDao.executSQLForJSONObject(sql, null, dbDataSourceID);


                if (result != null) {

                    String status = result.getString("ZCJG").toString().trim();

                    if (status.equals("AA")) {
                        json.put("RS", "TRUE");
                    } else if (status.equals("AE")) {
                        json.put("RS", "FALSE");
                        json.put("INFO", "注册出错," + result.getString("ZCJGSM"));
                    } else {
                        json.put("RS", "FALSE");
                        json.put("INFO", "注册流程出错!");
                    }

                } else {
                    json.put("RS", "FALSE");
                    json.put("INFO", "注册结果未保存到本地系统!");
                }
            } else {
                json.put("RS", "FALSE");
                json.put("INFO", "注册流程出错!");
            }
        } catch (Exception e) {
            LOGGER.error(e, e.fillInStackTrace());


            json.put("RS", "FALSE");
            json.put("INFO", e.fillInStackTrace());
        }
        return json;
    }

}
